package cn.rumoss.jvm.deep.gc;

public class GCParamAllocateTest {
	
	private static final int _1MB = 1024*1024;
	
	/**
	 * -Xms20M -Xms20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
	 * -Xms20M -Xms20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC
	 * (1)对象优先在Eden分配,如果不足,通过分配担保机制提前转移到老年代去
	 */
	public static void testAllocation() {
		byte[] a1,a2,a3,a4;
		a1 = new byte[2*_1MB];
		a2 = new byte[2*_1MB];
		a3 = new byte[1*_1MB];
		a4 = new byte[4*_1MB];// 出现一次 MinorGC
	}
	
	/**
	 * -Xms20M -Xms20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC -XX:PretenureSizeThreshold=3145728
	 * (2)大对象直接进入老年代
	 */
	public static void testPretenureSizeThreshold() {
		byte[] a;
		a = new byte[4*_1MB];// 直接分配在老年代中
	}

	/**
	 * -Xms20M -Xms20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution
	 * (3)长期存活的对象将进入老年代
	 */
	public static void testTenuringThreshold() {
		byte[] a1,a2,a3;
		a1 = new byte[_1MB/4];
		// 什么时候进入老年代取决于 XX:MaxTenuringThreshold设置
		a2 = new byte[4*_1MB];
		a3 = new byte[4*_1MB];
		a3 = null;
		a3 = new byte[4*_1MB];
	}
	
	/**
	 * -Xms20M -Xms20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC -XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution
	 * (4)动态对象年龄判定
	 */
	public static void testTenuringThreshold2() {
		byte[] a1,a2,a3,a4;
		a1 = new byte[_1MB/4];
		// a1+a2 大于survivo空间一半
		a2 = new byte[_1MB/4];
		a3 = new byte[4*_1MB];
		a4 = new byte[4*_1MB];
		a4 = null;
		a4 = new byte[4*_1MB];
	}
	
	/**
	 * -Xms20M -Xms20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC -XX:-HandlePromotionFailure
	 * (5)空间分配担保
	 */
	public static void testHandlePromotion() {
		byte[] a1,a2,a3,a4,a5,a6,a7;
		a1 = new byte[2*_1MB];
		a2 = new byte[2*_1MB];
		a3 = new byte[2*_1MB];
		a1 = null;
		a4 = new byte[2*_1MB];
		a5 = new byte[2*_1MB];
		a6 = new byte[2*_1MB];
		a4 = null;
		a5 = null;
		a6 = null;
		a7 = new byte[2*_1MB];
	}
	
	//public static void 
	
	public static void main(String[] args) {

		//testAllocation();
		//testPretenureSizeThreshold();
		//testTenuringThreshold();
		//testTenuringThreshold2();
		testHandlePromotion();
		
	}

}
